// 防抖：触发事件后n秒内只执行一次，如果在n秒内又触发了事件，则会重新计算函数执行时间
// 节流：连续触发事件，每n秒内执行一次

// 防抖
function debounce (callback, delay) {
  return function (event) {
    if (callback.hasOwnProperty('timeoutId')) {
      clearTimeout(callback.timeoutId)
    }

    callback.timeoutId = setTimeout(() => {
      callback.call(this, event)
      delete callback.timeoutId
    }, delay)
  }
}

// 节流
function throttle (callback, delay) {
  let pre = 0
  return function (event) {
    const current = Date.now()
    if (current - pre > delay) {
      callback.call(this, event)
      pre = current
    }
  }
}
